package com.iotechn.unimall.admin.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.SalesSendOperLogDO;
import com.iotechn.unimall.data.mapper.SalesSendOperLogMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class SalesSendOperLogServiceImpl implements SalesSendOperLogService {
    @Autowired
    private SalesSendOperLogMapper salesSendOperLogMapper;

     @Override
        public Page<SalesSendOperLogDO> list(Integer page, Integer limit, Long adminId) throws ServiceException {
            Wrapper<SalesSendOperLogDO> wrapper = new EntityWrapper<SalesSendOperLogDO>();
            /**if (!StringUtils.isEmpty(name)) {
                wrapper.like("username", name);
            }**/
            wrapper.orderBy("id", false);
            Integer count = salesSendOperLogMapper.selectCount(wrapper);
            List<SalesSendOperLogDO> SalesSendOperLogDOS = salesSendOperLogMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
            return new Page<>(SalesSendOperLogDOS, page, limit, count);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public SalesSendOperLogDO create(SalesSendOperLogDO salesSendOperLogDO, Long adminId) throws ServiceException {
//            salesSendOperLogDO.setAdminId(adminId);
            Date now=new Date();
            salesSendOperLogDO.setGmtCreate(now);
            salesSendOperLogDO.setGmtUpdate(now);
            if (salesSendOperLogMapper.insert(salesSendOperLogDO) > 0) {
                salesSendOperLogDO.setId(salesSendOperLogDO.getId());
                return salesSendOperLogDO;
            }
             throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public String update(SalesSendOperLogDO salesSendOperLogDO, Long adminId) throws ServiceException {
            Long id = salesSendOperLogDO.getId();
            if (id == null) {
               return "fail";
            }
            Date now=new Date();
            salesSendOperLogDO.setGmtUpdate(now);
            if (salesSendOperLogMapper.updateById(salesSendOperLogDO) > 0) {
                return "ok";
            }
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (salesSendOperLogMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

     }

    @Override
    public SalesSendOperLogDO detail(Long id, Long adminId) throws ServiceException {
       return salesSendOperLogMapper.selectById(id);
    }

}